//
//  payLimitViewController.m
//  SP2P
//
//  Created by Eternal on 2017/8/13.
//  Copyright © 2017年 EIMS. All rights reserved.
//

#import "payLimitViewController.h"
#import "LimitTableCell.h"
#define ButtonNumber    2

@interface payLimitViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{   // 畅捷
    UIButton *_cjButton;
    // 富友
    UIButton *_fyButton;
    UIView   *_buttonView;
    UIView   *_buttonLineView;
    UITableView *_cjTableView;
    UITableView *_fyTableView;
    // 数据表
    NSMutableArray *_dataArray;
    NSMutableArray *_imageArray;
    NSMutableArray *_cjlimitArray;
    NSMutableArray *_cjdayLimitArray;
    NSMutableArray *_fylimitArray;
    NSMutableArray *_fydayLimitArray;
    NSMutableArray *_isLowArray;
    NSMutableArray *_bankCodeArray;
    NSMutableArray *_isOpenBankArray;

}
@property (weak, nonatomic) IBOutlet UIView *cutView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation payLimitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (self.titleTag == 1) {
        [self initNavBarTitle:@"支持银行" andLeftItemName:@"返回" andRightItemName:@""];
    }else{
    [self initNavBarTitle:@"额度表" andLeftItemName:@"返回" andRightItemName:@""];
    }
    [self initData];
    [self initView];
}
- (void)initView{
     [self loadButtonView];
    _scrollView.frame = RECTMAKE(0, GetMaxY(_buttonView)+1, MSWIDTH, MSHIGHT - 64 - GetMaxY(_buttonView));
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(MSWIDTH*2, 0);
    _scrollView.pagingEnabled = YES;

    // 畅捷
    _cjTableView = [[UITableView alloc] initWithFrame:RECTMAKE(0, 0, MSWIDTH, GetHeight(_scrollView)) style:UITableViewStylePlain];
    _cjTableView.delegate = self;
    _cjTableView.dataSource = self;
    [_scrollView addSubview:_cjTableView];
    [_cjTableView registerNib:[UINib nibWithNibName:@"LimitTableCell" bundle:nil] forCellReuseIdentifier:@"changjie"];
    
    // 富友
    _fyTableView = [[UITableView alloc] initWithFrame:RECTMAKE(MSWIDTH, 0, MSWIDTH, GetHeight(_scrollView)) style:UITableViewStylePlain];
    _fyTableView.delegate = self;
    _fyTableView.dataSource = self;
    [_scrollView addSubview:_fyTableView];
    [_fyTableView registerNib:[UINib nibWithNibName:@"LimitTableCell" bundle:nil] forCellReuseIdentifier:@"fuyou"];
    

}
- (void)initData{
    _dataArray = [[NSMutableArray alloc] initWithArray:@[@"工商银行",@"建设银行",@"农业银行",@"中国银行",@"民生银行",@"光大银行",@"兴业银行",@"平安银行"]]; //,@"交通银行",@"邮政银行",@"中信银行",@"广发银行",@"浦发银行",@"北京银行",@"上海银行"
    _imageArray = [[NSMutableArray alloc] initWithArray:@[@"BankLogo_gsyh",@"BankLogo_jsyh",@"BankLogo_nyyh",@"BankLogo_zgyh",@"BankLogo_msyh",@"BankLogo_gdyh",@"BankLogo_xyyh",@"BankLogo_payh"]]; //,@"BankLogo_jtyh",@"BankLogo_yzyh",@"BankLogo_zxyh",@"BankLogo_gfyh",@"BankLogo_pfyh",@"BankLogo_bjyh",@"BankLogo_shyh"
    _cjlimitArray = [[NSMutableArray alloc] initWithArray:@[@"50000",@"20000",@"20000",@"50000",@"500000",@"500000",@"50000",@"500000"]]; //,@"50000",@"50000",@"5000",@"5000",@"5000"
    _cjdayLimitArray = [[NSMutableArray alloc] initWithArray:@[@"50000",@"20000",@"20000",@"500000",@"单日无限额",@"单日无限额",@"50000",@"1000000"]]; //,@"1000000",@"1000000",@"5000",@"5000",@"5000"
    _fylimitArray = [[NSMutableArray alloc] initWithArray:@[@"100000",@"50000",@"50000",@"50000",@"100000",@"100000",@"50000",@"100000"]]; //,@"50000",@"50000",@"5000",@"5000",@"5000"
    _fydayLimitArray = [[NSMutableArray alloc] initWithArray:@[@"400000",@"100000",@"200000",@"200000",@"200000",@"200000",@"50000",@"200000"]]; //,@"1000000",@"1000000",@"5000",@"5000",@"5000"
    _isLowArray = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]]; //,@"",@"2",@"",@"",@""
    _isOpenBankArray = [[NSMutableArray alloc] initWithArray:@[@"",@"",@"",@"",@"",@"",@"",@""]]; //,@"",@"",@"",@"",@""
    //    _bankCodeArray = [[NSMutableArray alloc] initWithArray:@[@"102",@"105",@"301",@"305",@"303",@"302",@"309",@"410",@"104",@"103",@"401",@"310",@"403"]]; //
    _bankCodeArray = [[NSMutableArray alloc] initWithArray:@[@"ICBC",@"CCB",@"ABC",@"BOC",@"CMBC",@"CEB",@"CIB",@"SZPAB"]]; //,@"BCOM",@"PSBC",@"CITIC",@"GDB",@"SPDB",@"BCCB",@"BOS"

 
}
- (void)loadButtonView {
    
    CGFloat scrollOriginY = KAdaptNum(45);
    
    _buttonView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH, scrollOriginY) backgroundColor:[UIColor whiteColor] superview:self.view];
    
    _cjButton = [UIButton buttonWithFrame:CGRectMake(0, 0, MSWIDTH/ButtonNumber, scrollOriginY - 1) backgroundColor:nil title:@"畅捷支付" titleColor:KColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    _fyButton = [UIButton buttonWithFrame:CGRectMake(MSWIDTH/ButtonNumber, 0, MSWIDTH/ButtonNumber, GetHeight(_cjButton)) backgroundColor:nil title:@"富友支付" titleColor:KBlackColor titleFont:14.0 target:self action:@selector(headerButtonClick:) superview:_buttonView];
    
    
    UIView *buttonLine = [UIView viewWithFrame:CGRectMake(0, CGRectGetMaxY(_cjButton.frame), MSWIDTH, _buttonView.frame.size.height - CGRectGetMaxY(_cjButton.frame)) backgroundColor:nil superview:_buttonView];
    
    _buttonLineView = [UIView viewWithFrame:CGRectMake(0, 0, MSWIDTH/ButtonNumber, GetHeight(buttonLine)) backgroundColor:KColor superview:buttonLine];
    
}
#pragma mark - Action
- (void)headerButtonClick:(UIButton *)button {
    if (button == _cjButton) {
        
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = 0;
            _buttonLineView.frame = frame;
            _scrollView.contentOffset = CGPointMake(0, 0);
            
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }else if(button == _fyButton){
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = MSWIDTH/ButtonNumber;
            _buttonLineView.frame = frame;
            _scrollView.contentOffset = CGPointMake(MSWIDTH, 0);
        } completion:^(BOOL finished) {
            [self buttonChange:button];
        }];
    }
}
- (void)buttonChange:(UIButton *)button {
    if (button == _cjButton) {
        [_cjButton setTitleColor:KColor forState:UIControlStateNormal];
        _cjButton.titleLabel.font = KFontBoldSize(14.0);
        
        [_fyButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _fyButton.titleLabel.font = KFontSize(14.0);
        
    }else if (button == _fyButton) {
        [_cjButton setTitleColor:KBlackColor forState:UIControlStateNormal];
        _cjButton.titleLabel.font = KFontSize(14.0);
        
        [_fyButton setTitleColor:KColor forState:UIControlStateNormal];
        _fyButton.titleLabel.font = KFontBoldSize(14.0);
    }
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count>0?_dataArray.count:1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataArray.count) {
        LimitTableCell *cjCell = nil;
        LimitTableCell *fyCell = nil;
        if (tableView == _cjTableView) {
            cjCell = [tableView dequeueReusableCellWithIdentifier:@"changjie"];
        }else{
            
            fyCell = [tableView dequeueReusableCellWithIdentifier:@"fuyou"];
        }
       
       cjCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *bankNameStr = _dataArray[indexPath.row];
        cjCell.bankNameLabel.text = bankNameStr;
        fyCell.bankNameLabel.text = bankNameStr;
        if ([bankNameStr rangeOfString:@"建设银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jsyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jsyh"];
        }else if ([bankNameStr rangeOfString:@"工商银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gsyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gsyh"];
        }else if ([bankNameStr rangeOfString:@"农业银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_nyyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_nyyh"];
        }else if ([bankNameStr rangeOfString:@"中国银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zgyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zgyh"];
        }else if ([bankNameStr rangeOfString:@"交通银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jtyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_jtyh"];
        }else if ([bankNameStr rangeOfString:@"兴业银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_xyyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_xyyh"];
        }else if ([bankNameStr rangeOfString:@"中信银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zxyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zxyh"];
        }else if ([bankNameStr rangeOfString:@"光大银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gdyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gdyh"];
        }else if ([bankNameStr rangeOfString:@"平安银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_payh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_payh"];
        }else if ([bankNameStr rangeOfString:@"邮政银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_yzyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_yzyh"];
        }else if ([bankNameStr rangeOfString:@"上海银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shyh"];
        }else if ([bankNameStr rangeOfString:@"浦发银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_pfyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_pfyh"];
        }else if ([bankNameStr rangeOfString:@"民生银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_msyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_msyh"];
        }else if ([bankNameStr rangeOfString:@"招商银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zsyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_zsyh"];
        }else if ([bankNameStr rangeOfString:@"广发银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gfyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_gfyh"];
        }else if ([bankNameStr rangeOfString:@"上海农商银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shnsyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_shnsyh"];
        }else if ([bankNameStr rangeOfString:@"北京银行"].length != 0) {
            cjCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_bjyh"];
            fyCell.logoImageView.image = [UIImage imageNamed:@"BankLogo_bjyh"];
        }
        
        NSString *mindStr = @"";
        cjCell.bankMindLabel.hidden = [_isOpenBankArray[indexPath.row] isEqualToString:@"1"]?NO:YES;
        fyCell.bankMindLabel.hidden = [_isOpenBankArray[indexPath.row] isEqualToString:@"1"]?NO:YES;
        if ([_isLowArray[indexPath.row] isEqualToString:@"1"]) {
            if ([_isOpenBankArray[indexPath.row] isEqualToString:@"1"]) {
                mindStr = @"*单笔单日额度较低不建议使用*需开通银联在线支付http://online.unionpay.com/static/open/";
            }else {
                mindStr = @"*单笔单日额度较低不建议使用";
            }
        }else if ([_isLowArray[indexPath.row] isEqualToString:@"2"]) {
            mindStr = @"近期单笔最高2万，单日最高2万";
        }else {
            if ([_isOpenBankArray[indexPath.row] isEqualToString:@"1"]) {
                if ([bankNameStr rangeOfString:@"浦发银行"].length != 0) {
                    mindStr = @"*需在浦发网上银行内点击开通银联在线支付";
                }else {
                    mindStr = @"*需开通银联在线支付http://online.unionpay.com/static/open/";
                }
            }else {
                mindStr = @"";
            }
        }
        CGFloat mindHeight = 0.0f;
        if (mindStr.length <= 0) {
            cjCell.mindLabel.hidden = YES;
            fyCell.mindLabel.hidden = YES;
            mindHeight = KAdaptNum(18);
        }else {
            cjCell.mindLabel.hidden = NO;
            fyCell.mindLabel.hidden = NO;
            mindHeight = [MyTools getSizeOfString:mindStr maxWidth:MSWIDTH - 10 - KAdaptNum(115) maxHeight:1000 withFontSize:10].height;
            if (mindHeight < KAdaptNum(18)) {
                mindHeight = KAdaptNum(18);
            }
        }
        cjCell.bankNameLabel.frame = RECTMAKE(KAdaptNum(50), KAdaptNum(8), KAdaptNum(65), KAdaptNum(18));
        cjCell.mindLabel.frame = RECTMAKE(GetMaxX(cjCell.bankNameLabel), KAdaptNum(10), MSWIDTH - 10 - GetMaxX(cjCell.bankNameLabel), mindHeight);
        cjCell.mindLabel.text = mindStr;
        cjCell.limitAmountLabel.frame = RECTMAKE(KAdaptNum(50), GetMaxY(cjCell.mindLabel), MSWIDTH - 10 - KAdaptNum(50), KAdaptNum(20));
        cjCell.logoImageView.frame = RECTMAKE(KAdaptNum(10), (GetMaxY(cjCell.limitAmountLabel) + 5 - KAdaptNum(30))/2, KAdaptNum(30), KAdaptNum(30));
        
        fyCell.bankNameLabel.frame = RECTMAKE(KAdaptNum(50), KAdaptNum(8), KAdaptNum(65), KAdaptNum(18));
        fyCell.mindLabel.frame = RECTMAKE(GetMaxX(cjCell.bankNameLabel), KAdaptNum(10), MSWIDTH - 10 - GetMaxX(cjCell.bankNameLabel), mindHeight);
        fyCell.mindLabel.text = mindStr;
        fyCell.limitAmountLabel.frame = RECTMAKE(KAdaptNum(50), GetMaxY(fyCell.mindLabel), MSWIDTH - 10 - KAdaptNum(50), KAdaptNum(20));
        fyCell.logoImageView.frame = RECTMAKE(KAdaptNum(10), (GetMaxY(fyCell.limitAmountLabel) + 5 - KAdaptNum(30))/2, KAdaptNum(30), KAdaptNum(30));
        NSString *limitStrrrrr = nil;
        NSString *dayLimitStrrrrr = nil;
        if (tableView == _cjTableView) {
           limitStrrrrr = _cjlimitArray[indexPath.row];
           dayLimitStrrrrr = _cjdayLimitArray[indexPath.row];
        }else{
         
           limitStrrrrr = _fylimitArray[indexPath.row];
           dayLimitStrrrrr = _fydayLimitArray[indexPath.row];
        }
        
        if (limitStrrrrr.doubleValue >= 10000) {
            limitStrrrrr = [NSString stringWithFormat:@"%.0f万",limitStrrrrr.doubleValue/10000.0];
        }
       
        if (dayLimitStrrrrr.doubleValue >= 10000) {
            dayLimitStrrrrr = [NSString stringWithFormat:@"%.0f万",dayLimitStrrrrr.doubleValue/10000.0];
        }
        
        NSString *oncelimitStr = [NSString stringWithFormat:@"单笔最高%@,",limitStrrrrr];
        NSString *dayLimitStr = nil;
        if ([dayLimitStrrrrr isEqualToString:@"单日无限额"]) {
             dayLimitStr = [NSString stringWithFormat:@"%@",dayLimitStrrrrr];
        }else{
            dayLimitStr = [NSString stringWithFormat:@"单日最高%@",dayLimitStrrrrr];
        }
       
        NSString *limitStr = [NSString stringWithFormat:@"%@%@",oncelimitStr,dayLimitStr];
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:limitStr];
        [attributedString addAttribute:NSFontAttributeName value:KFontSize(10) range:NSMakeRange(4, oncelimitStr.length-1-4)];
        [attributedString addAttribute:NSFontAttributeName value:KFontSize(10) range:NSMakeRange(oncelimitStr.length+4, limitStr.length-oncelimitStr.length-4)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:KColor range:NSMakeRange(4, oncelimitStr.length-1-4)];
        [attributedString addAttribute:NSForegroundColorAttributeName value:KColor range:NSMakeRange(oncelimitStr.length+4, limitStr.length-oncelimitStr.length-4)];
        cjCell.limitAmountLabel.attributedText = attributedString;
        cjCell.limitAmountLabel.adjustsFontSizeToFitWidth = YES;
        fyCell.limitAmountLabel.attributedText = attributedString;
        fyCell.limitAmountLabel.adjustsFontSizeToFitWidth = YES;
        cjCell.frame=CGRectMake(cjCell.frame.origin.x, cjCell.frame.origin.y, cjCell.frame.size.width, GetMaxY(cjCell.limitAmountLabel) + 5);
        fyCell.frame=CGRectMake(fyCell.frame.origin.x, fyCell.frame.origin.y, fyCell.frame.size.width, GetMaxY(fyCell.limitAmountLabel) + 5);
        if (tableView == _cjTableView) {
            return cjCell;
        }else{
            return fyCell;
        }
        
    }else {
        EmptyTableViewCell *cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"emptyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = KbackgroundColor;
        
        cell.frame=CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, tableView.frame.size.height);
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _scrollView) {
        double OriginX = scrollView.contentOffset.x;
        if (OriginX>=0 && OriginX<=MSWIDTH) {
            CGRect frame = _buttonLineView.frame;
            frame.origin.x = (OriginX/MSWIDTH)*(MSWIDTH/ButtonNumber);
            _buttonLineView.frame = frame;
            
            if (OriginX <= MSWIDTH/2) {
                [self buttonChange:_cjButton];
            }else {
                [self buttonChange:_fyButton];
            }
        }else {
        }
    }
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
